<?php
// +----------------------------------------------------------------------
// | 吉智云科技 [ 简单 高效 卓越 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://baijiahao.codewe.cn/admin.php All rights reserved.
// +----------------------------------------------------------------------
// | Author: 下雨吧จุ๊บ <642445992@qq.com>
// +----------------------------------------------------------------------
namespace Admin\Controller;

use lyf\Page;

/**
 * 用户控制器
 * @author 下雨吧จุ๊บ <642445992@qq.com>
 */
class AccountController extends AdminController
{
    /**
     * 账号列表
     * @author 下雨吧จุ๊บ <642445992@qq.com>
     */
    public function index()
    {
        // 搜索
        $keyword                                  = I('keyword', '', 'string');
        $condition                                = array('like', '%' . $keyword . '%');
        $map['id|username|name|email|mobile'] = array(
            $condition,
            $condition,
            $condition,
            $condition,
            $condition,
            '_multi' => true,
        );
        // 获取所有账号
        $p             = !empty($_GET["p"]) ? $_GET['p'] : 1;
        $map['status'] = array('egt', '0'); // 禁用和正常状态
        $uid = $_SESSION['ly_admin_']['user_auth']['uid'];
        $zhanghao   = D('account');
        $data_list     = $zhanghao
            ->page($p, C('ADMIN_PAGE_ROWS'))
            ->where($map)
            ->order('id desc')
            ->select();
        foreach ($data_list as $k=>$v) {
            $data_list[$k]['member'] = D('User')->find($v['mid'])['nickname'];
            $data_list[$k]['plat'] = D('platform')->find($v['platid'])['platname'];
            $where1['aid'] = $v['id'];
            $where1['userid'] = $uid;
            $account_user   = M('account_user');
            $account_user_info = $account_user
                ->where($where1)
                ->find();
            if (empty($account_user_info)){
                unset($data_list[$k]);
            }
        }
        $data_list1     = $zhanghao
            ->select();
        $num = 0;
        foreach ($data_list1 as $k=>$v) {
            $where1['aid'] = $v['id'];
            $where1['userid'] = $uid;
            $account_user   = M('account_user');
            $account_user_info = $account_user
                ->where($where1)
                ->find();
            if (!empty($account_user_info)){
                $num++;
            }
        }
        $page = new Page(
            $num,
            C('ADMIN_PAGE_ROWS')
        );
        $pid = D('User')->find($uid)['pid'];
        if ($pid == 0){
            //添加自定义按钮
            $attr['name']  = 'edit2';
            $attr['title'] = '运营';
            $attr['class'] = 'label label-success';
            $attr['href']  = U('operate', array('id' => '__data_id__'));
            // 使用Builder快速建立列表页面
            $builder = new \lyf\builder\ListBuilder();
            $builder->setMetaTitle('账号列表') // 设置页面标题
            ->addTopButton('addnew') // 添加新增按钮
            ->addTopButton('resume') // 添加启用按钮
            ->addTopButton('forbid') // 添加禁用按钮
            ->addTopButton('delete') // 添加删除按钮
            ->setSearch('请输入ID/用户名／邮箱／手机号', U('index'))
                ->addTableColumn('id', 'ID')
//                ->addTableColumn('image', '头像', 'picture')
                ->addTableColumn('name', '账号名称')
                ->addTableColumn('plat', '平台')
                ->addTableColumn('username', '用户名')
                ->addTableColumn('member', '运营者')
                ->addTableColumn('email', '邮箱')
                ->addTableColumn('mobile', '手机号')
                ->addTableColumn('create_time', '注册时间', 'time')
                ->addTableColumn('update_time', '更新时间', 'time')
                ->addTableColumn('status', '状态', 'status')
                ->addTableColumn('right_button', '操作', 'btn')
                ->setTableDataList($data_list) // 数据列表
                ->setTableDataPage($page->show()) // 数据列表分页
                ->addRightButton('edit') // 添加编辑按钮
                ->addRightButton('forbid') // 添加禁用/启用按钮
                ->addRightButton('delete') // 添加删除按钮
//                ->addRightButton('recycle') // 添加回收按钮
                ->addRightButton('self', $attr) // 添加运营按钮
                ->display();
        }else{
            // 使用Builder快速建立列表页面
            $builder = new \lyf\builder\ListBuilder();
            $builder->setMetaTitle('账号列表') // 设置页面标题
            ->addTopButton('addnew') // 添加新增按钮
            ->addTopButton('resume') // 添加启用按钮
            ->addTopButton('forbid') // 添加禁用按钮
            ->addTopButton('delete') // 添加删除按钮
            ->setSearch('请输入ID/用户名／邮箱／手机号', U('index'))
                ->addTableColumn('id', 'ID')
//                ->addTableColumn('image', '头像', 'picture')
                ->addTableColumn('name', '账号名称')
                ->addTableColumn('plat', '平台')
                ->addTableColumn('username', '用户名')
                ->addTableColumn('email', '邮箱')
                ->addTableColumn('mobile', '手机号')
                ->addTableColumn('create_time', '注册时间', 'time')
                ->addTableColumn('update_time', '更新时间', 'time')
                ->addTableColumn('status', '状态', 'status')
                ->addTableColumn('right_button', '操作', 'btn')
                ->setTableDataList($data_list) // 数据列表
                ->setTableDataPage($page->show()) // 数据列表分页
                ->addRightButton('edit') // 添加编辑按钮
                ->addRightButton('forbid') // 添加禁用/启用按钮
                ->addRightButton('delete') // 添加删除按钮
//                ->addRightButton('recycle') // 添加回收按钮
                ->display();
        }
    }

    /**
     * 分配运营者
     * @author 下雨吧จุ๊บ <642445992@qq.com>
     */
    public function operate($id)
    {
        //分配,取消
        if (!empty($_GET['distribution'])){
            $account_user   = M('account_user');
            $data['aid'] = $_GET['id'];
            $data['userid'] = $_GET['userid'];
            if ($_GET['distribution'] == 1){
                $account_user->add($data);
            }else{
                $result = $account_user->where($data)->delete();
            }
        }
        // 搜索
        $keyword                                  = I('keyword', '', 'string');
        $condition                                = array('like', '%' . $keyword . '%');
        $map['id|username|nickname|email|mobile'] = array(
            $condition,
            $condition,
            $condition,
            $condition,
            $condition,
            '_multi' => true,
        );
        // 获取所有员工
        $p             = !empty($_GET["p"]) ? $_GET['p'] : 1;
        $map['status'] = array('egt', '0'); // 禁用和正常状态
        $user   = D('admin_user');
        $data_list     = $user
            ->page($p, C('ADMIN_PAGE_ROWS'))
            ->where($map)
            ->order('id desc')
            ->select();
        $page = new Page(
            $user->where($map)->count(),
            C('ADMIN_PAGE_ROWS')
        );
        foreach ($data_list as $k=>$v) {
            $where['aid'] = $id;
            $where['userid'] = $v['id'];
            $account_user   = M('account_user');
            $account_user_info = $account_user
                ->where($where)
                ->find();
            if (empty($account_user_info)){
                $data_list[$k]['status'] = '<span style="color: #d3283b;">未分配</span>';
            }else{
                $data_list[$k]['status'] = '<span style="color: #1DC116;">已分配</span>';
            }
        }
        //添加自定义按钮
        $right['name']  = 'edit2';
        $right['title'] = '分配';
        $right['class'] = 'label label-success';
        $right['href']  = U('operate', array('userid' => '__data_id__','id' => $id,'p' => $p,'keyword' => $keyword,'distribution' => 1));
        //添加自定义按钮
        $right1['name']  = 'edit3';
        $right1['title'] = '取消';
        $right1['class'] = 'label label-danger';
        $right1['href']  = U('operate', array('userid' => '__data_id__','id' => $id,'p' => $p,'keyword' => $keyword,'distribution' => 2));
        // 使用Builder快速建立列表页面
        $builder = new \lyf\builder\ListBuilder();
        $builder->setMetaTitle('员工列表') // 设置页面标题
        ->setSearch('请输入ID/用户名／邮箱／手机号', U('operate', array('id' => $id)))
            ->addTableColumn('id', 'ID')
            ->addTableColumn('nickname', '账号名称')
            ->addTableColumn('username', '用户名')
            ->addTableColumn('email', '邮箱')
            ->addTableColumn('mobile', '手机号')
            ->addTableColumn('status', '分配状态')
            ->addTableColumn('create_time', '注册时间', 'time')
            ->addTableColumn('update_time', '更新时间', 'time')
            ->addTableColumn('right_button', '操作', 'btn')
            ->setTableDataList($data_list) // 数据列表
            ->setTableDataPage($page->show()) // 数据列表分页
            ->addRightButton('self', $right)// 添加分配按钮
            ->addRightButton('self', $right1)// 添加取消分配按钮
            ->display();
    }

    /**
     * 分配运营者
     * @author 下雨吧จุ๊บ <642445992@qq.com>
     */
    public function operate1($id)
    {
        if (request()->isPost()) {
            // 提交数据
            $zhanghao = D('account');
            $data        = $zhanghao->create();
            $data['update_time'] = time();
            $data['mid'] += 1;
            if ($data) {
                $result = $zhanghao
                    ->field('id,name,username,pwd,email,mobile,image,update_time,cookie,mid')
                    ->save($data);
                if ($result) {
                    $this->success('更新成功', U('index'));
                } else {
                    $this->error('更新失败：' . $zhanghao->getError());
                }
            } else {
                $this->error($zhanghao->getError());
            }
        } else {
            // 获取账号信息
            $info = D('account')->find($id);
            //获取角色账号列表
            $user = D('user')->select();
            foreach ($user as $k => $v) {
                if ($v['pid'] == $_SESSION['ly_admin_']['user_auth']['uid']){
                    $user_list[$k]['label'] = $v['nickname'];
                    $user_list[$k]['value'] = $v['id'];
                }
            }
            $this->display('operate');
            // 使用FormBuilder快速建立表单页面
            $builder = new \lyf\builder\FormBuilder();
            $builder->setMetaTitle('分配运营者') // 设置页面标题
            ->setPostUrl(U('operate')) // 设置表单提交地址
            ->addFormItem('id', 'hidden', 'ID', 'ID')
                ->setFormData($info)
                ->addFormItem('mid', 'select', '所属角色', '',$user_list)
                ->display();
        }
    }

    /**
     * 新增用户
     * @author 下雨吧จุ๊บ <642445992@qq.com>
     */
    public function add()
    {
        if (request()->isPost()) {
            $zhanghao = D('account');
            $data        = $zhanghao->create();
            if (empty($data['name']) || empty($data['username']) || empty($data['pwd']) || empty($data['platid'])){
                $this->error('请填写完整信息');
            }
            $data['create_time'] = time();
            $data['update_time'] = time();
            if ($data) {
                $uid = $_SESSION['ly_admin_']['user_auth']['uid'];
                $user_object = D('User');
                $where->id = $uid;
                $user_data = $user_object->where($where)->find();
                $data['mid'] = $uid;
                $data['userid'] = $user_data['pid'];
                if ($user_data['pid'] == 0){
                    $data['userid'] = $uid;
                }
                $id = $zhanghao->add($data);
                if ($id) {
                    $this->success('新增成功', U('index'));
                } else {
                    $this->error('新增失败：' . $zhanghao->getError());
                }
            } else {
                $this->error($zhanghao->getError());
            }
        } else {
//            //获取角色账号列表
//            $user = D('user')->select();
//            foreach ($user as $k => $v) {
//                $user_list[$k]['label'] = $v['nickname'];
//                $user_list[$k]['value'] = $v['id'];
//            }

            //获取平台列表
            $plat = D('platform')->select();
            foreach ($plat as $k => $v) {
                $plat_list[$k+1]['label'] = $v['platname'];
            }

            // 使用FormBuilder快速建立表单页面
            $builder = new \lyf\builder\FormBuilder();
            $builder->setMetaTitle('新增账号') //设置页面标题
                ->setPostUrl(U('add')) //设置表单提交地址
                ->addFormItem('name', 'text', '账号名称', '账号名称', '', array('must' => 1))
                ->addFormItem('username', 'text', '用户名', '用户名', '', array('must' => 1))
                ->addFormItem('pwd', 'password', '密码', '密码', '', array('must' => 1))
//                ->addFormItem('userid', 'select', '所属角色', '不同角色对应相应的权限',$user_list, array('must' => 1))
                ->addFormItem('platid', 'select', '平台', '账号所属平台',$plat_list, array('must' => 1))
                ->addFormItem('email', 'text', '邮箱', '邮箱')
                ->addFormItem('mobile', 'text', '手机号', '手机号')
//                ->addFormItem('cookie', 'textarea', 'COOKIE值', 'COOKIE值')
                ->addFormItem('image', 'picture', '头像', '头像')
                ->setFormData(array('reg_type' => 'admin'))
                ->display();
        }
    }

    /**
     * 编辑用户
     * @author 下雨吧จุ๊บ <642445992@qq.com>
     */
    public function edit($id)
    {
        if (request()->isPost()) {
            // 密码为空表示不修改密码
            if ($_POST['pwd'] === '') {
                unset($_POST['pwd']);
            }
            // 提交数据
            $zhanghao = D('account');
            $data        = $zhanghao->create();
            $data['update_time'] = time();
            if ($data) {
                $result = $zhanghao
                    ->field('id,name,username,pwd,email,mobile,image,update_time,cookie')
                    ->save($data);
                if ($result) {
                    $this->success('更新成功', U('index'));
                } else {
                    $this->error('更新失败：' . $zhanghao->getError());
                }
            } else {
                $this->error($zhanghao->getError());
            }
        } else {
            // 获取账号信息
            $info = D('account')->find($id);
//            //获取角色账号列表
//            $user = D('user')->select();
//            foreach ($user as $k => $v) {
//                $user_list[$k]['label'] = $v['nickname'];
//                $user_list[$k]['value'] = $v['id'];
//            }

//            //获取平台列表
//            $plat = D('platform')->select();
//            foreach ($plat as $k => $v) {
//                $plat_list[$k]['label'] = $v['platname'];
//                $plat_list[$k]['value'] = $v['id'];
//            }

            // 使用FormBuilder快速建立表单页面
            $builder = new \lyf\builder\FormBuilder();
            $builder->setMetaTitle('编辑用户') // 设置页面标题
                ->setPostUrl(U('edit')) // 设置表单提交地址
                ->addFormItem('id', 'hidden', 'ID', 'ID')
                ->addFormItem('name', 'text', '账号名称', '账号名称', '', array('must' => 1))
                ->addFormItem('username', 'text', '用户名', '用户名', '', array('must' => 1))
                ->addFormItem('pwd', 'password', '密码', '密码', '', array('must' => 1))
//                ->addFormItem('userid', 'select', '所属角色', '不同角色对应相应的权限',$user_list, array('must' => $info['userid']))
//                ->addFormItem('platid', 'select', '平台', '账号所属平台',$plat_list, array('must' => $info['platid']),'readonly')
                ->addFormItem('email', 'text', '邮箱', '邮箱')
                ->addFormItem('mobile', 'text', '手机号', '手机号')
//                ->addFormItem('cookie', 'textarea', 'COOKIE值', 'COOKIE值',array('must' => 1))
                ->addFormItem('image', 'picture', '头像', '头像')
                ->setFormData($info)
                ->display();
        }
    }

    /**
     * 设置一条或者多条数据的状态
     * @author 下雨吧จุ๊บ <642445992@qq.com>
     */
    public function setStatus($model = '', $strict = null)
    {
        if ('' == $model) {
            $model = request()->controller();
        }
        $ids = I('request.ids');
        if (is_array($ids)) {
            if (in_array('1', $ids)) {
                $this->error('超级管理员不允许操作');
            }
        } else {
            if ($ids === '1') {
                $this->error('超级管理员不允许操作');
            }
        }
        parent::setStatus($model);
    }
}
